package com.rhzy.wnhc.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rhzy.wnhc.entity.PersonParam;
import com.rhzy.wnhc.entity.TJobWish;
import com.rhzy.wnhc.entity.TPerson;
import com.rhzy.wnhc.entity.TrainWish;
import com.rhzy.wnhc.service.TJobWishService;
import com.rhzy.wnhc.service.TPersonService;
import com.rhzy.wnhc.util.AjaxResult;
import com.rhzy.wnhc.util.CheckIdcardIsLegal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "小程序端接口-用工信息录入")
@RestController
@RequestMapping("/person")
public class FrontController {
    @Autowired
    TPersonService service;
    @Autowired
    TJobWishService jobWishService;
    @ApiOperation(value = "用工信息录入", tags = {"小程序端接口-用工信息录入",})
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Transactional
    public AjaxResult add(@RequestBody @Validated PersonParam param){
        try{

            TPerson person=param.getPerson();
            person.setIdCard(person.getIdCard().toUpperCase());
            if(!CheckIdcardIsLegal.checkIdCard(person.getIdCard())){
                return AjaxResult.addError("身份证格式错误");
            }
            if(!CheckIdcardIsLegal.checkPhone(person.getPhone())){
                return AjaxResult.addError("手机号码格式错误");
            }
            TPerson dbPerson=service.getOne(new LambdaQueryWrapper<TPerson>().eq(TPerson::getIdCard,person.getIdCard()));
            if(dbPerson==null){
                service.save(person);
            }else {
                person.setId(dbPerson.getId());
                service.updateById(person);
            }
            if(person.getOutTarinStatus()==true){
                TrainWish trainWish=param.getTrainWish();
                if(trainWish==null){
                    return AjaxResult.addError("没有乘车信息！");
                }
                if(StringUtils.isEmpty(trainWish.getOutType())
                        ||StringUtils.isEmpty(trainWish.getOutDate())
                        ||StringUtils.isEmpty(trainWish.getOnCarAddress())
                        ||StringUtils.isEmpty(trainWish.getHealthProve())){
                    return AjaxResult.addError("有必填项没填完整！");
                }
                TJobWish jobWish=processTrainWish(param);
                jobWish.setPId(person.getId());
                TJobWish isExist=jobWishService.getOne(new LambdaQueryWrapper<TJobWish>().eq(TJobWish::getIdCard,jobWish.getIdCard()).last(" limit 1"));
                if(isExist!=null){
                    jobWish.setId(isExist.getId());
                    jobWishService.updateById(jobWish);
                }else{
                    jobWishService.save(jobWish);
                }

            }

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.addError("异常："+e.getMessage());
        }
        return AjaxResult.addSuccess("录入成功");
    }

    private TJobWish processTrainWish(PersonParam param) {
        TJobWish jobWish=new TJobWish();
        BeanUtils.copyProperties(param.getTrainWish(),jobWish);
        BeanUtils.copyProperties(param.getPerson(),jobWish);
        return jobWish;
    }

    @ApiOperation(value = "乘车信息录入", tags = {"小程序端接口-用工信息录入",})
    @RequestMapping(value = "/addWishJob", method = RequestMethod.POST)
    @Transactional
    public AjaxResult addWishJob(@RequestBody @Validated TJobWish jobWish){
        try{

            if(!CheckIdcardIsLegal.checkPhone(jobWish.getPhone())){
                return AjaxResult.addError("手机号码格式错误");
            }
            jobWishService.save(jobWish);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.addError("异常："+e.getMessage());
        }
        return AjaxResult.addSuccess("录入成功");
    }
}
